package com.KOL.model;

import com.KOL.present.TileType;
import java.awt.*;
import java.awt.image.*;

public class TileOccupier {
  private String name;
  private TileType type;      // BLOCK, ITEM, or ENTITY
  private BufferedImage image;
  private int xTile, yTile;
  private int xDraw, yDraw;

  private TiledEntity entity = null;

  public TileOccupier(String nm, TileType tt, int x, int y, BufferedImage im,
                          int xRowStart, int yRowStart,
                          int xTileWidth, int yTileHeight) {
    name = nm;
    type = tt;
    xTile = x; yTile = y;
    image = im;
    calcPosition(xRowStart, yRowStart, xTileWidth, yTileHeight);
  }

  private void calcPosition(int xRowStart, int yRowStart,
                          int xTileWidth, int yTileHeight) {

    int xImOffset = xTileWidth/2 - image.getWidth()/2;
    int yImOffset = yTileHeight - image.getHeight() - yTileHeight/5;

    xDraw = xRowStart + (xTile * xTileWidth) + xImOffset;
    if (yTile%2 == 0)
      yDraw = yRowStart + (yTile/2 * yTileHeight) + yImOffset;
    else
      yDraw = yRowStart + ((yTile-1)/2 * yTileHeight) + yImOffset;
  }

  public String getName()
  { return name; }

  public TileType getType()
  {  return type; }

  public Point getTileLoc()
  {  return new Point(xTile, yTile);  }


  public void addEntityRef(TiledEntity s)
  { if (type == TileType.ENTITY)
      entity = s;
  }
  public void draw(Graphics g, int xOffset, int yOffset) {
    if (type == TileType.ENTITY) {
      entity.setPosition( xDraw+xOffset, yDraw+yOffset);
      entity.drawSprite(g);
    }
    else // the entity is a ITEM or BLOCK
      g.drawImage( image, xDraw+xOffset, yDraw+yOffset, null);
  }
}




